/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.facilscrum.form.tareas;

import javax.swing.table.DefaultTableModel;
import org.facilscrum.beans.Equiposusuarios;
import org.facilscrum.beans.Estatustareas;
import org.javscaz.javscaztools.utilidades.Utilidades;
import org.facilscrum.beans.Tareas;
import org.facilscrum.beans.Userstories;
import org.facilscrum.beans.Usuarios;
import org.facilscrum.controller.EquiposusuariosController;
import org.facilscrum.controller.EstatustareasController;
import org.facilscrum.controller.TareasController;
import org.facilscrum.controller.UserstoriesController;
import org.facilscrum.controller.UsuariosController;
import org.facilscrum.generales.Mensajeria;
import org.facilscrum.sesion.MySesion;

/**
 *
 * @author avbravo
 */
public class TaskUpdateForm extends javax.swing.JInternalFrame {

    Estatustareas estatustareas = new Estatustareas();
    EstatustareasController estatustareasController = new EstatustareasController();
    EquiposusuariosController equiposusuariosController = new EquiposusuariosController();
    Tareas tareas = new Tareas();
    TareasController tareasController = new TareasController();
    UserstoriesController userstoriesController = new UserstoriesController();
    Userstories userstories = new Userstories();
    UsuariosController usuariosController = new UsuariosController();
    Usuarios usuarios = new Usuarios();
    DefaultTableModel modelo;

    /**
     * Creates new form BacklogForm
     */
    public TaskUpdateForm() {
        initComponents();
        modelo = (DefaultTableModel) jTable1.getModel();
        jTable1.setEnabled(true);
        jButtonGuardar.setEnabled(false);
        cargarTablaTareas();
        if (!cargarComboUsuarios() || !cargarComboUserStories() || !cargarComboEstatustareas()) {
            jComboBoxUsuarios.setEditable(false);
            jTable1.setEnabled(false);
        }
        if (!MySesion.getSeleccionoProyectoTrabajo()) {
            Mensajeria.MensajeWarning("Debe indicar el proyecto de trabajo");
        } else {
            if (cargarComboUsuarios()) {
                jButtonGuardar.setEnabled(true);
            }
            jTextFieldProyecto.setText(MySesion.getProyectos().getProyecto());
        }
        cargarTablaTareas();
        limpiar();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldId = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldTarea = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldDuraccion = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jButtonGuardar = new javax.swing.JButton();
        jButtonCerrar = new javax.swing.JButton();
        jButtonLimpiar = new javax.swing.JButton();
        jButtonEliminar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaDescripcion = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        jComboBoxUsuarios = new javax.swing.JComboBox();
        jTextFieldProyecto = new javax.swing.JTextField();
        jComboBoxUserstories = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jComboBoxEstatus = new javax.swing.JComboBox();

        setClosable(true);
        setTitle("Actualizar Tareas");

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Proyecto");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "Tarea", "Duracion", "Estatus", "usuario"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(60);
        jTable1.getColumnModel().getColumn(2).setMaxWidth(100);

        jLabel2.setText("Id");

        jTextFieldId.setEnabled(false);

        jLabel3.setText("Tarea");

        jTextFieldTarea.setEnabled(false);

        jLabel4.setText("Duracion");

        jTextFieldDuraccion.setEnabled(false);

        jLabel5.setText("horas");

        jButtonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/jscrum/gif/save_icon.gif"))); // NOI18N
        jButtonGuardar.setToolTipText("Guardar");
        jButtonGuardar.setEnabled(false);
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jButtonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/jscrum/gif/return.gif"))); // NOI18N
        jButtonCerrar.setToolTipText("Cerrar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/jscrum/gif/clear.png"))); // NOI18N
        jButtonLimpiar.setToolTipText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        jButtonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/jscrum/gif/delete.gif"))); // NOI18N
        jButtonEliminar.setToolTipText("Eliminar");
        jButtonEliminar.setEnabled(false);
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });

        jLabel6.setText("Descripcion");

        jTextAreaDescripcion.setColumns(20);
        jTextAreaDescripcion.setRows(5);
        jTextAreaDescripcion.setEnabled(false);
        jScrollPane2.setViewportView(jTextAreaDescripcion);

        jLabel9.setText("Usuario");

        jComboBoxUsuarios.setEnabled(false);

        jTextFieldProyecto.setEnabled(false);

        jComboBoxUserstories.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxUserstoriesActionPerformed(evt);
            }
        });

        jLabel8.setText("User Stories");

        jLabel10.setText("Estatus");

        jComboBoxEstatus.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldProyecto, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(400, 538, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(24, 24, 24)
                                .addComponent(jComboBoxUserstories, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel6)
                                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING))
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel10))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jComboBoxUsuarios, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldTarea, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jTextFieldDuraccion, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel5))
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jComboBoxEstatus, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(129, 129, 129)
                .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldProyecto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxUserstories, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldTarea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldDuraccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addGap(7, 7, 7)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel10)
                        .addGap(131, 131, 131))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBoxUsuarios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBoxEstatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(7, 7, 7)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonCerrar, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButtonEliminar, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButtonLimpiar)
                            .addComponent(jButtonGuardar))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonCerrarActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        try {
            Integer fila = jTable1.getSelectedRow();
            if (fila == -1) {
                Mensajeria.Mensaje("No se selecciono ninguna fila");

                return;
            }
            tareas.setIdtarea(Integer.parseInt(jTable1.getValueAt(fila, 0).toString()));
            Tareas t = tareasController.findByPrimaryKey(tareas);
            if (t != null) {
                tareas = t;
            } else {
                Mensajeria.MensajeWarning("No existe una tarea con ese codigo");
                limpiar();
                return;
            }
            jTextFieldId.setText(tareas.getIduserstories().toString());
            jTextFieldTarea.setText(tareas.getTask());
            jTextFieldDuraccion.setText(tareas.getDuracion().toString());
            jTextAreaDescripcion.setText(tareas.getDescripcion());

            jComboBoxUsuarios.setSelectedItem(tareas.getUsuarios().getNombre() + " - " + tareas.getUsuarios().getCedula());


            jComboBoxEstatus.setSelectedItem(tareas.getEstatustareas().getEstatustareas());
habilitar(true);

            jButtonGuardar.setEnabled(true);
            jButtonEliminar.setEnabled(true);
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "MouseClicked()");
        }
    }//GEN-LAST:event_jTable1MouseClicked

    private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed
        actualizar();
       
    }//GEN-LAST:event_jButtonGuardarActionPerformed

    private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed
        limpiar();
        habilitar(false);
    }//GEN-LAST:event_jButtonLimpiarActionPerformed

    private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed
        eliminar();
         habilitar(false);
    }//GEN-LAST:event_jButtonEliminarActionPerformed

    private void jComboBoxUserstoriesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxUserstoriesActionPerformed
        try {
            jTextFieldId.setText("");
            jTextFieldTarea.setText("");
            jTextFieldDuraccion.setText("");
            jTextAreaDescripcion.setText("");
            int j = jComboBoxUserstories.getSelectedIndex();
            if (j != -1) {
                userstories = userstoriesController.getUserstorieslist().get(j);
                tareas.setIduserstories(userstories.getIduserstories());
                cargarTablaTareas();
            } else {
                Mensajeria.Mensaje("No se ha seleccionado ningun user stories");

            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "combo()");
        }
    }//GEN-LAST:event_jComboBoxUserstoriesActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JComboBox jComboBoxEstatus;
    private javax.swing.JComboBox jComboBoxUserstories;
    private javax.swing.JComboBox jComboBoxUsuarios;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextAreaDescripcion;
    private javax.swing.JTextField jTextFieldDuraccion;
    private javax.swing.JTextField jTextFieldId;
    private javax.swing.JTextField jTextFieldProyecto;
    private javax.swing.JTextField jTextFieldTarea;
    // End of variables declaration//GEN-END:variables

    private Boolean cargarComboUsuarios() {
        try {

            jComboBoxUsuarios.removeAllItems();
            String sql = "idequipo='" + MySesion.getProyectos().getIdequipo() + "'";
            if (equiposusuariosController.query(sql)) {
                for (Equiposusuarios eu : equiposusuariosController.getEquiposusuarioslist()) {
                    jComboBoxUsuarios.addItem(eu.getUsuarios().getNombre() + " - " + eu.getUsuarios().getCedula());
                }
                return true;
            } else {
                Mensajeria.MensajeWarning("No hay usuarios asignados al equipo de este proyecto");
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "cargarComboUsuarios() ");
        }
        return false;
    }

    private Boolean cargarComboUserStories() {
        try {

            jComboBoxUserstories.removeAllItems();
            String where = "idproyecto='" + MySesion.getProyectos().getIdproyecto() + "'";
            if (userstoriesController.query(where)) {
                jComboBoxUserstories.removeAllItems();
                for (Userstories u : userstoriesController.getUserstorieslist()) {
                    jComboBoxUserstories.addItem(u.getIduserstories() + " " + u.getNombre());
                }
                return true;
            } else {
                Mensajeria.Mensaje("Debe crear primero los user stories ");
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "cargarComboUserStories()");

        }
        return false;
    }

    private Boolean cargarComboEstatustareas() {
        try {

            jComboBoxEstatus.removeAllItems();
            if (estatustareasController.selectAll()) {
                for (Estatustareas e : estatustareasController.getEstatustareaslist()) {
                    jComboBoxEstatus.addItem(e.getEstatustareas());
                }
                return true;
            } else {
                Mensajeria.Mensaje("Debe crear primero los estatus de tareas");
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "cargarComboEstatustareas()");

        }
        return false;
    }
    /*
     * 
     */

    private Boolean guiToData() {
        try {
            if (jTextFieldTarea.getText() == null || jTextFieldTarea.getText().equals("")) {
                Mensajeria.Mensaje("El nombre  no debe estar vacio");
                jTextFieldTarea.requestFocus();
                return false;
            }
            if (jTextFieldDuraccion.getText() == null || jTextFieldDuraccion.getText().equals("")) {
                Mensajeria.Mensaje("La importancia no debe estar vacio");
                jTextFieldDuraccion.requestFocus();
                return false;
            }


            if (jTextAreaDescripcion.getText() == null || jTextAreaDescripcion.getText().equals("")) {
                Mensajeria.Mensaje("Como probarlo  no debe estar vacio");
                jTextAreaDescripcion.requestFocus();
                return false;
            }

            if (Integer.parseInt(jTextFieldDuraccion.getText()) <= 0) {
                Mensajeria.MensajeWarning("La importancia debe ser mayor de cero");
                jTextFieldDuraccion.requestFocus();
                return false;
            }


            tareas.setDuracion(Integer.parseInt(jTextFieldDuraccion.getText()));
            tareas.setDescripcion(jTextAreaDescripcion.getText());
            tareas.setIdestatustareas("asignado");
            tareas.setFecha(MySesion.getFechausername());
            tareas.setTask(jTextFieldTarea.getText());
            int j = jComboBoxUserstories.getSelectedIndex();
            if (j != -1) {
                Userstories u = userstoriesController.getUserstorieslist().get(j);
                tareas.setIduserstories(u.getIduserstories());
            } else {
                Mensajeria.Mensaje("No se ha seleccionado ningun user stories");
                return false;
            }
            int k = jComboBoxUsuarios.getSelectedIndex();
            if (k != -1) {
                //  Usuarios usuarios = usuariosController.getUsuarioslist().get(k);
                Equiposusuarios e = equiposusuariosController.getEquiposusuarioslist().get(k);
                tareas.setUsername(e.getUsername());
            } else {
                Mensajeria.Mensaje("No se ha seleccionado ningun usuario");
                return false;
            }
            int m = jComboBoxEstatus.getSelectedIndex();
            if (m != -1) {
                //  Usuarios usuarios = usuariosController.getUsuarioslist().get(k);
                Estatustareas et = estatustareasController.getEstatustareaslist().get(m);
                tareas.setIdestatustareas(et.getIdestatustareas());
            } else {
                Mensajeria.Mensaje("No se ha seleccionado ningun estatus de tareas");
                return false;
            }
            return true;
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "actualizar()");

        }
        return false;
    }

    private void actualizar() {
        try {
            userstories = userstoriesController.findByPrimaryKey(userstories);
            if (!guiToData()) {
                return;
            }

            if (tareasController.update(tareas)) {
                Mensajeria.Mensaje("Se actualizo la tarea");

                limpiar();
                cargarTablaTareas();
            }


        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "actualizar()");

        }
    }

    private void eliminar() {
        try {
            if (!guiToData()) {
                return;
            }
            if (Mensajeria.PreguntaSiNo("Desea eliminar user tareas", "Eliminar").equals("no")) {
                return;
            }
            if (tareasController.delete(tareas)) {
                Mensajeria.Mensaje("Se elimino user tareas");
                limpiar();
                cargarTablaTareas();
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "eliminar()");
        }
    }

    private void cargarTablaTareas() {
        try {

            modelo = Utilidades.limpiarModelo(modelo);
            String where = "iduserstories='" + userstories.getIduserstories() + "'";
            if (tareasController.query(where)) {
                for (Tareas t : tareasController.getTareaslist()) {
                    Object[] filas = new Object[5]; // Hay tres columnas en la tabla       
                    filas[0] = t.getIdtarea();
                    filas[1] = t.getTask();
                    filas[2] = t.getDuracion();
                    filas[3] = t.getEstatustareas().getEstatustareas();
                    filas[4] = t.getUsuarios().getUsername();
                    modelo.addRow(filas);
                }
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "cargarTablaTareas()");
        }
    }

    private void limpiar() {
        jTextFieldId.setText("");
        jTextFieldTarea.setText("");
        jTextFieldDuraccion.setText("");
        jTextAreaDescripcion.setText("");
        jButtonGuardar.setEnabled(false);
        jButtonEliminar.setEnabled(false);
    }
    
    private void habilitar(Boolean b){
        jTextFieldTarea.setEnabled(b);
jTextFieldDuraccion.setEnabled(b);
jTextAreaDescripcion.setEnabled(b);
jComboBoxUsuarios.setEnabled(b);
jComboBoxEstatus.setEnabled(b);
    }
}
